package com.metagurukul.metagranth.actionservlets;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.metagurukul.metagranth.service.AdminQuestionService;


/**
 * This Servlet holds the functionality of approving answers by the admin panel on the adminquestiondisplay page.
 * It will retrieve the name of the Radio Button clicked for a particular answer of a particular question.
 * It will then call the closeQuestionApproveAnswer(answerId) function in the service class which performs
 * two functions-Approving that Question and closing that question.
 * if both functions are successfully performed it will be redirected to the successpage
 * else will be directed to errorPage.
 * @author team MetaGranth
 *
 */
public class AdminApproveAnswerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminApproveAnswerServlet() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPerform(request, response);
	}
	
	/**
	 * this function will get the name of the radio button of a particular answer which the admin selected for approval 
	 * of answer of a particular question.
	 * it will then call for the closeQuestionApproveAnswer function in the service class for approving the answers
	 * and closing the questions
	 * if the status returned is true then it is redirected to success.jsp 
	 * else is redirected to errorPage.jsp
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */

	private void doPerform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		if(session.getAttribute("userId")!=null){
		try {
			//get the answerId of an answer selected by admin
			int answerId = Integer.parseInt(request.getParameter("checkanswer"));
			// calls closeQuestionApproveAnswer function in the service layer for closing Questions and approving answer
			boolean status = AdminQuestionService.closeQuestionApproveAnswer(answerId);
			RequestDispatcher rd = null;
			if (status == true) {
				//on success directs to success.jsp
				rd=request.getRequestDispatcher("success.jsp");
			} else {
				//else sends to errorPage.jsp
				request.setAttribute("errorMessage","Oops Something Went Wrong!! :-( Try Again Later");
				rd = request.getRequestDispatcher("errorPage.jsp");
			}

			rd.forward(request, response);

		} catch (Exception exception) {
			request.setAttribute("errormsg", exception.getMessage());
			request.getRequestDispatcher("errorPage.jsp").forward(request,response);
		}
		} else {
			response.sendRedirect("index.jsp");
	}
	}
}
